package com.zsrd.self.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.zsrd.self.base.Result;
import com.zsrd.self.base.vo.SmParamVo;
import com.zsrd.self.base.vo.SmReturnVo;
import com.zsrd.self.util.MySM2Util;


/**
 * <pre>
 *  sm的加密解密Controller
 * </pre>
 *
 * @author sunjiyong
 * @since 2023/6/19 17:07
 */

public class BaseController {

    //解密参数转成bean
    public  static  <T> T decryptParamToBean(SmParamVo smVo, Class<T> clazz) {
        try {
            return JSONObject.parseObject(MySM2Util.decrypt(smVo.getIespsm()), clazz);
        }catch (Exception e){
            return null;
        }
    }


    //解密参数转成字符串
    public  static  String decryptParamToString(String data) {
        try {
            JSONObject json= JSONObject.parseObject(data);
            return MySM2Util.decrypt(json.getString("iespsm"));
        }catch (Exception e){
            return null;
        }
    }


    //加密出参
    public  static SmReturnVo encryptReturn(Result res){
        SmReturnVo smReturnVo=new SmReturnVo();
        smReturnVo.setIespData(MySM2Util.encrypt(JSONObject.toJSONString(res)));
        return smReturnVo;
    }


    //解密参数测试控制台打印
    public  static  <T> T decryptParamToBeanTest(SmParamVo smVo, Class<T> clazz) {
        String param=MySM2Util.decrypt(smVo.getIespsm());
        System.out.println("所传参数解密为："+param);
        JSONObject.parseArray(param,clazz);
        return JSONObject.parseObject(param, clazz);
    }


    public  static  <T> T decryptParamToBeanList(SmParamVo smVo, Class<T> clazz) {
        String param=MySM2Util.decrypt(smVo.getIespsm());
        System.out.println("所传参数解密为："+param);
        JSONObject.parseArray(param,clazz);
        return (T) JSONObject.parseArray(param,clazz);
    }



    //解密参数转成字符串控制台打印
    public  static  String decryptParamToStringTest(String data) {
        try {
            JSONObject json= JSONObject.parseObject(data);
            String param=json.getString("iespsm");
            System.out.println("所传参数解密为："+param);
            return MySM2Util.decrypt(param);
        }catch (Exception e){
            return null;
        }
    }
}
